﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using IGGCore.Data;

namespace IGGCore
{
    public class InjuriesParser : BaseParser
    {
        /// <summary>
        /// Parse standings
        /// </summary>
        public void ParseInjuries(Scoreboard scoreboard)
        {
            Injuries injs = new Injuries();
            string url = "http://www.cbssports.com/mlb/injuries";
            string transContent = "";
            StreamReader reader = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                reader = new StreamReader(request.GetResponse().GetResponseStream());
                transContent = reader.ReadToEnd();
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            transContent = transContent.Substring(transContent.IndexOf("<div id=\"pageRow\">"));

            string[] data = transContent.Split(new string[] { "<table class=\"data\"" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string datastr in data)
            {
                if (datastr.Contains("mlb/teams/page"))
                {
                    string tag = "mlb/teams/page/";
                    string content = datastr.Substring(datastr.IndexOf(tag) + tag.Length);
                    string team = content.Substring(0, content.IndexOf("/"));
                    string[] entries = content.Split(new string[] { "<tr class=\"row1\"", "<tr class=\"row2\"" }, StringSplitOptions.RemoveEmptyEntries);
                    // row1 - row2

                    for (int i = 1; i < entries.Length; i++)
                    {
                        string contentsr = entries[i].Substring(entries[i].IndexOf("%\">")+3);
                        string lastUpdated = contentsr.Substring(0, contentsr.IndexOf("</td>"));
                        contentsr = contentsr.Substring(contentsr.IndexOf("%\">") + 3);
                        string name = contentsr.Substring(0, contentsr.IndexOf("</td>"));
                    }
                }
            }

        }
    }
}
